package com.xmall.msg.base;

import cn.hutool.core.util.IdUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class BaseMsg implements Serializable {

    /**
     * 消息标识
     */
    private String correlationKey;

    /**
     * 来源
     */
    private String source;

    /**
     * 交换机
     */
    private String exchange;

    /**
     * 路由键
     */
    private String routingKey;

    /**
     * 创建时间
     */
    private Date createTime = new Date();

    private void setSource(String source) {
        this.source = source;
    }

    private void setCorrelationKey(String correlationKey) {
        this.correlationKey = correlationKey;
    }

    private void setExchange(String exchange) {
        this.exchange = exchange;
    }

    private void setRoutingKey(String routingKey) {
        this.routingKey = routingKey;
    }

    private void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public BaseMsg build(MsgSourceType msgSourceType, String exchange, String routingKey) {
        this.correlationKey = IdUtil.getSnowflake(msgSourceType.getId()).nextIdStr();
        this.source = msgSourceType.getName();
        this.exchange = exchange;
        this.routingKey = routingKey;
        return this;
    }
}
